//
//  HLHomeViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLHomeViewController.h"
#import <JXCategoryView/JXCategoryView.h>
#import "HLHomeLabelRequest.h"
#import "HLHomeListViewController.h"
#import "HLHomeFlowViewController.h"
#import "HLLiveFemaleViewController.h"
#import "HLHomeVideoViewController.h"
#import "HLHomeLoveBellView.h"
#import "HLHomeDiscountView.h"
#import "HLDiscountRequest.h"

@interface HLHomeViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray<HLHomeLabelModel *>*labels;
@property (nonatomic, assign) NSInteger videoIndex;
@property (nonatomic, assign) UIStatusBarStyle statusStyle;
@property (nonatomic, strong) HLHomeDiscountView *discountView;

@end

@implementation HLHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.statusStyle = UIStatusBarStyleDarkContent;
    self.request = [[HLHomeLabelRequest alloc] initWithType:HLHomeLabelTypeHome];
    self.request.delegate = self;
    [self requestInit];
    [self setupDiscountView];
}

- (void)refresh
{
    [self requestInit];
}

- (void)setupDiscountView
{
    if ([HLRunTime shareInstance].isFood) {
        return;
    }
    HLDiscountRequest *homeListRequest = [[HLDiscountRequest alloc] init];
    [homeListRequest startWithCompletionBlockWithSuccess:^(__kindof HLDiscountRequest * _Nonnull request) {
        if (request.isSuccess){
            self.discountView = [[HLHomeDiscountView alloc] init];
            [self.view addSubview:self.discountView];
            [self.discountView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(140, 68));
                make.bottom.mas_equalTo(-(SafeAreaInsetsConstantForDeviceWithNotch.bottom + 68));
            }];
            self.discountView.cycleDatas = [request discountList];
        }
    } failure:nil];
}


- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    HLHomeLabelRequest *labelRequest = (HLHomeLabelRequest *)self.request;
    
    self.labels = [labelRequest labelList];
    self.categoryView.titles = [self.labels qmui_mapWithBlock:^NSString *(HLHomeLabelModel *item, NSInteger index) {
        return item.tagName;
    }];
    [HLRunTime shareInstance].labels = self.labels;
    if ([HLRunTime shareInstance].isFood) {
        self.categoryView.defaultSelectedIndex = 1;
    }
    
    [self.categoryView reloadData];
}

- (void)initSubviews
{
    [super initSubviews];
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectZero];
    self.categoryView.delegate = self;
    self.categoryView.titleColor = HLTextColor2;
    self.categoryView.titleSelectedColor = HLTextColor1;
    self.categoryView.titleFont = UIFontMake(16);
    self.categoryView.titleSelectedFont = UIFontMediumMake(20);
    self.categoryView.averageCellSpacingEnabled = NO;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.titleLabelStrokeWidthEnabled = YES;
    self.categoryView.cellSpacing = 25;
    self.categoryView.contentEdgeInsetLeft = 18;
    self.categoryView.backgroundColor = UIColorClear;
    [self.view addSubview:self.categoryView];
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(38);
        make.top.mas_equalTo(StatusBarHeightConstant + 4);
    }];
    
    self.categoryView.titleColorGradientEnabled = YES;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorClear;
    lineView.indicatorHeight = 5.f;
    lineView.indicatorWidth = 12.f;
    lineView.indicatorCornerRadius = 1.5f;
    lineView.verticalMargin = 2;
    lineView.lineStyle = JXCategoryIndicatorLineStyle_Lengthen;
    UIImageView *bottom = [[UIImageView alloc] initWithImage:UIImageMake(@"tag_bottom")];
    [lineView addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    self.categoryView.indicators = @[lineView];
    

    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-TabBarHeight);
    }];
    
    self.categoryView.listContainer = self.listContainerView;
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.statusStyle;
}

#pragma mark - JXCategoryViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.labels.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    HLHomeLabelModel *label = self.labels[index];
    
    if ([label.tagCode isEqualToString:@"short_video"]){
        self.videoIndex = index;
        HLHomeVideoViewController *listVC = [[HLHomeVideoViewController alloc] initWithLabel:label];
        return listVC;
    } else if ([label.tagCode isEqualToString:@"default_fc"]){
        HLHomeListViewController *listVC = [[HLHomeListViewController alloc] initWithLabel:label];
        return listVC;
    } else if ([label.tagCode isEqualToString:@"live"]){
        HLLiveFemaleViewController *listVC = [[HLLiveFemaleViewController alloc] initWithLabel:label];
        return listVC;
    } else {
        if ([label.showType isEqualToString:@"1"]){
            HLHomeListViewController *listVC = [[HLHomeListViewController alloc] initWithLabel:label];
            return listVC;
        }
        HLHomeFlowViewController *listVC = [[HLHomeFlowViewController alloc] initWithLabel:label];
        return listVC;
    }
}

/**
 滚动选中的情况才会调用该方法

 @param categoryView categoryView对象
 @param index 选中的index
 */
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index
{
    
    [self videoCurrentWithIndex:index];
}

/**
 控制能否点击Item

 @param categoryView categoryView对象
 @param index 准备点击的index
 @return 能否点击
 */
- (BOOL)categoryView:(JXCategoryBaseView *)categoryView canClickItemAtIndex:(NSInteger)index
{
    [self videoCurrentWithIndex:index];
    return  YES;
}


- (void)videoCurrentWithIndex:(NSInteger)index
{
    HLHomeLabelModel *label = self.labels[index];
    if ([label.tagCode isEqualToString:@"short_video"]){
        self.discountView.hidden = YES;
        self.categoryView.titleColor = [UIColorWhite colorWithAlphaComponent:0.6];
        self.categoryView.titleSelectedColor = UIColorWhite;
        self.view.backgroundColor = UIColorBlack;
        self.statusStyle = UIStatusBarStyleLightContent;
    } else {
        self.discountView.hidden = NO;
        self.categoryView.titleColor = HLTextColor2;
        self.categoryView.titleSelectedColor = HLTextColor1;
        self.view.backgroundColor = UIColorWhite;
        self.statusStyle = UIStatusBarStyleDarkContent;
    }
    [self.categoryView reloadCellAtIndex:index];
    if (self.videoIndex > 0){
        [self.categoryView reloadCellAtIndex:self.videoIndex];
    }
    [self.navigationController setNeedsStatusBarAppearanceUpdate];
}



@end
